/*
Copyright (c) 2008, Simon Lebettre
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    * Neither the name of the rpginventory nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.rpginventory.hud.dnd;

import java.awt.dnd.DnDConstants;

import javax.swing.ImageIcon;
import javax.swing.JLabel;

import org.rpginventory.model.BodySlot;
import org.rpginventory.model.Item;

import com.jmex.awt.swingui.JMEDesktopState;
import com.jmex.awt.swingui.dnd.JMEDndException;
import com.jmex.awt.swingui.dnd.JMEDragAndDrop;
import com.jmex.awt.swingui.dnd.JMEDragGestureEvent;
import com.jmex.awt.swingui.dnd.JMEDragGestureListener;
import com.jmex.awt.swingui.dnd.JMEDragSourceEvent;
import com.jmex.awt.swingui.dnd.JMEDragSourceListener;
import com.jmex.awt.swingui.dnd.JMEDropTargetEvent;
import com.jmex.awt.swingui.dnd.JMEDropTargetListener;
import com.jmex.awt.swingui.dnd.JMEMouseDragGestureRecognizer;

/**
 * pass the effect as tooltiptext
 * 
 * @author Administrator
 * 
 */
public class DndIcon extends JLabel implements JMEDragGestureListener, JMEDragSourceListener, JMEDropTargetListener {

	private static final long serialVersionUID = 1L;
	
	private JMEDragAndDrop dndSupport;
	private int dndConstantsAction;

	private Item item;

	private BodySlot bodySlot;

	public DndIcon(JMEDesktopState desktopSate, Item item, BodySlot b) {
		this(desktopSate, item, DnDConstants.ACTION_COPY_OR_MOVE, b);
	}

	public DndIcon(JMEDesktopState desktopSate, Item item, int dndConstantsAction, BodySlot b) {
		this.dndConstantsAction = dndConstantsAction;
		this.bodySlot = b;
		if (item != null) {
			this.setIcon(item.getIcon());
			this.item = item;
		}
		

		this.dndSupport = desktopSate.getDesktop().getDragAndDropSupport();

		new JMEMouseDragGestureRecognizer(dndSupport, this, dndConstantsAction, this);
	}

	public DndIcon(JMEDesktopState desktopSate) {
		this(desktopSate, null, null);
	}

	public void dragGestureRecognized(JMEDragGestureEvent dge) {
		if (this.getIcon() == null) {
			return;
		}

		TransferableItem transferable = new TransferableItem(this.item);

		try {
			dndSupport.startDrag(dge, (ImageIcon) this.getIcon(), transferable, this);
		} catch (JMEDndException e) {

			e.printStackTrace();
		}
		// this.setIcon(null);

	}

	public void dragDropEnd(JMEDragSourceEvent e) {
	}

	public void dragEnter(JMEDragSourceEvent e) {

	}

	public void dragExit(JMEDragSourceEvent e) {
	}

	public void dragEnter(JMEDropTargetEvent e) {
	}

	public void dragExit(JMEDropTargetEvent e) {

	}

	public void dragOver(JMEDropTargetEvent e) {

	}

	public void drop(JMEDropTargetEvent e) {

	
		TransferableItem t = (TransferableItem) e.getTransferable();

		if (t.getItem().getAllowedSlots().contains(this.bodySlot)) {
			//contains on an enumset is a binary operation, very efficient
			// only swap icon with source if we are not making a link
			if (this.dndConstantsAction != DnDConstants.ACTION_LINK) {
				DndIcon source = (DndIcon) e.getSource();
				source.setIcon(this.getIcon());
				source.setItem(this.getItem());
			}
			
			if ( this.bodySlot.equals(BodySlot.LRING1)){
				this.setIcon(t.getItem().getIcon16());				
			} else {
				this.setIcon(t.getIcon());	
			}
						
			this.setItem(t.getItem());
			
			

		}
	}

	public Item getItem() {
		return item;
	}

	public void setItem(Item item) {
		this.item = item;
	}

	public BodySlot getBodySlot() {
		return bodySlot;
	}

	public void setBodySlot(BodySlot bodySlot) {
		this.bodySlot = bodySlot;
	}
}
